// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'package:hawk/hawk.dart';
import 'package:nim_core/nim_core.dart';

import '../main_test.dart';
import 'method_name_value.dart';

class HandleUserCase extends HandleBaseCase {
  HandleUserCase();

  @override
  Future<ResultBean?> handle(event) async {
    super.handle(event);

    var isCallback = false;

    if (className?.trim() != 'UserService') {
      return null;
    }

    var inputParams = Map<String, dynamic>();
    if (params != null && params!.length > 0) {
      inputParams = params![0] as Map<String, dynamic>;
    }
    switch (methodName?.trim()) {
      case 'onUserInfoChange':
        {
          isCallback = true;
          subsriptions.add(
              NimCore.instance.userService.onUserInfoChange.listen((event) {
            print('UserService#onUserInfoChange $event');
            IntegratedManager.instance.report(
                'onUserInfoChange',
                ResultBean(
                    code: 0, data: event.map((e) => e.toMap()).toList()));
          }));
          break;
        }
      case 'onFriendAddedOrUpdated':
        {
          isCallback = true;
          subsriptions.add(NimCore.instance.userService.onFriendAddedOrUpdated
              .listen((event) {
            print('UserService#onFriendAddedOrUpdated $event');
            IntegratedManager.instance.report(
                'onFriendAddedOrUpdated',
                ResultBean(
                    code: 0, data: event.map((e) => e.toMap()).toList()));
          }));
          break;
        }
      case 'onFriendDeleted':
        {
          isCallback = true;
          subsriptions
              .add(NimCore.instance.userService.onFriendDeleted.listen((event) {
            print('UserService#onFriendDeleted $event');
            IntegratedManager.instance
                .report('onFriendDeleted', ResultBean(code: 0, data: event));
          }));
          break;
        }
      case 'onBlackListChanged':
        {
          isCallback = true;
          subsriptions.add(
              NimCore.instance.userService.onBlackListChanged.listen((event) {
            print('UserService#onBlackListChanged ');
            IntegratedManager.instance
                .report('onBlackListChanged', ResultBean(code: 0, data: null));
          }));
          break;
        }
      case 'onMuteListChanged':
        {
          isCallback = true;
          subsriptions.add(
              NimCore.instance.userService.onMuteListChanged.listen((event) {
            print('UserService#onMuteListChanged $event');
            IntegratedManager.instance.report('onMuteListChanged',
                ResultBean(code: 0, data: muteListChangedNotify(event)));
          }));
          break;
        }
      case getUserInfo:
        NIMResult<NIMUser> result = await NimCore.instance.userService
            .getUserInfo(inputParams['userId'] as String);
        handleCaseResult = ResultBean(
            code: result.code, data: result.data?.toMap(), message: methodName);
        break;
      case fetchUserInfoList:
        NIMResult<List<NIMUser>> result = await NimCore.instance.userService
            .fetchUserInfoList((inputParams['userIdList'] as List<dynamic>)
                .map((e) => e.toString())
                .toList());
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toMap()).toList(),
            message: methodName);
        break;
      case 'getUserInfoListAndroid':
        NIMResult<List<NIMUser>> result = await NimCore.instance.userService
            .getUserInfoListAndroid((inputParams['userIdList'] as List<dynamic>)
                .map((e) => e.toString())
                .toList());
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toMap()).toList(),
            message: methodName);
        break;
      case 'getAllUserInfoAndroid':
        NIMResult<List<NIMUser>> result =
            await NimCore.instance.userService.getAllUserInfoAndroid();
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toMap()).toList(),
            message: methodName);
        break;
      case 'searchAccountByAliasAndroid':
        NIMResult<List<String>> result = await NimCore.instance.userService
            .searchAccountByAliasAndroid(inputParams['alias'] as String);
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toString()).toList(),
            message: methodName);
        break;
      case 'searchFriendsByKeywordAndroid':
        NIMResult<List<NIMFriend>> result = await NimCore.instance.userService
            .searchFriendsByKeywordAndroid(inputParams['keyword'] as String);
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toMap()).toList(),
            message: methodName);
        break;
      case 'getFriendAccountsAndroid':
        NIMResult<List<String>> result =
            await NimCore.instance.userService.getFriendAccountsAndroid();
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toString()).toList(),
            message: methodName);
        break;
      case updateMyUserInfo:
        NIMUser user = NIMUser.fromMap(inputParams);
        NIMResult<void> result =
            await NimCore.instance.userService.updateMyUserInfo(user);
        handleCaseResult = ResultBean(
            code: result.code, data: user.toMap(), message: methodName);
        break;
      case searchUserIdListByNick:
        NIMResult<List<String>> result = await NimCore.instance.userService
            .searchUserIdListByNick(inputParams['nick']);
        handleCaseResult = ResultBean(
            code: result.code, data: result.data, message: methodName);
        break;
      case searchUserInfoListByKeyword:
        NIMResult<List<NIMUser>> result = await NimCore.instance.userService
            .searchUserInfoListByKeyword(inputParams['keyword']);
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toMap()).toList(),
            message: methodName);
        break;
      case getFriendList:
        NIMResult<List<NIMFriend>> result =
            await NimCore.instance.userService.getFriendList();
        handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.map((e) => e.toMap()).toList(),
            message: methodName);
        break;
      case 'getFriend':
        var result =
            await NimCore.instance.userService.getFriend(inputParams['userId']);
        handleCaseResult = ResultBean(
            code: result.code, data: result.data?.toMap(), message: methodName);
        break;
      case addFriend:
        NIMResult<void> result = await NimCore.instance.userService.addFriend(
            userId: inputParams['userId'],
            verifyType:
                NIMVerifyType.values.elementAt(inputParams['verifyType'] ?? 0));
        handleCaseResult =
            ResultBean(code: result.code, data: null, message: methodName);
        break;
      case ackAddFriend:
        NIMResult<void> result = await NimCore.instance.userService
            .ackAddFriend(
                userId: inputParams['userId'], isAgree: inputParams['isAgree']);
        handleCaseResult =
            ResultBean(code: result.code, data: null, message: methodName);
        break;
      case deleteFriend:
        NIMResult<void> result = await NimCore.instance.userService
            .deleteFriend(
                userId: inputParams['userId'],
                includeAlias: inputParams['includeAlias']);
        handleCaseResult =
            ResultBean(code: result.code, data: null, message: methodName);
        break;
      case updateFriend:
        NIMResult<void> result = await NimCore.instance.userService
            .updateFriend(
                userId: inputParams['userId'], alias: inputParams['alias']);
        handleCaseResult =
            ResultBean(code: result.code, data: null, message: methodName);
        break;
      case isMyFriend:
        NIMResult<bool> result = await NimCore.instance.userService
            .isMyFriend(inputParams['userId']);
        handleCaseResult = ResultBean(
            code: result.code, data: result.data, message: methodName);
        break;
      case getBlackList:
        NIMResult<List<String>> result =
            await NimCore.instance.userService.getBlackList();
        handleCaseResult = ResultBean(
            code: result.code, data: result.data, message: methodName);
        break;
      case addToBlackList:
        NIMResult<void> result = await NimCore.instance.userService
            .addToBlackList(inputParams['userId']);
        handleCaseResult =
            ResultBean(code: result.code, data: null, message: methodName);
        break;
      case removeFromBlackList:
        NIMResult<void> result = await NimCore.instance.userService
            .removeFromBlackList(inputParams['userId']);
        handleCaseResult =
            ResultBean(code: result.code, data: null, message: methodName);
        break;
      case isInBlackList:
        NIMResult<bool> result = await NimCore.instance.userService
            .isInBlackList(inputParams['userId']);
        handleCaseResult = ResultBean(
            code: result.code, data: result.data, message: methodName);
        break;
      case getMuteList:
        NIMResult<List<String>> result =
            await NimCore.instance.userService.getMuteList();
        handleCaseResult = ResultBean(
            code: result.code, data: result.data, message: methodName);
        break;
      case setMute:
        NIMResult<void> result = await NimCore.instance.userService.setMute(
            userId: inputParams['userId'], isMute: inputParams['isMute']);
        handleCaseResult =
            ResultBean(code: result.code, data: null, message: methodName);
        break;
      case isMute:
        NIMResult<bool> result =
            await NimCore.instance.userService.isMute(inputParams['userId']);
        handleCaseResult = ResultBean(
            code: result.code, data: result.data, message: methodName);
        break;
      default:
        break;
    }

    if (isCallback) {
      handleCaseResult = ResultBean.success(data: null);
    }

    return handleCaseResult;
  }

  Map<String, dynamic> muteListChangedNotify(NIMMuteListChangedNotify notify) {
    return <String, dynamic>{"account": notify.account, "mute": notify.mute};
  }
}
